#include "codeports.h"

void
CP_Init(void)
{
	printf("CodePorts\n");
	srand(time(NULL));

	SCR_Init();
	KEY_Init();
	FMT_Init();
	PL_Init();
}

void
CP_Final(void)
{
	PL_Final();
	KEY_Final();
	FMT_Final();
	SCR_Final();
}

#ifdef CP_MAIN

int
main(int argc, char **argv)
{
	#define splits (1 << 2)
	DEMO demos[splits * splits];
	BOX boxes[splits * splits];
	unsigned int i, j;

	CP_Init();

	for(j = 0; j < splits; j++)
	{
		for(i = 0; i < splits; i++)
		{
			BOX_Allocate(&boxes[j * splits + i], cp.video.mode.current.xres / splits, cp.video.mode.current.yres / splits);
			plugins[j * splits + i].DEMO_Init(&demos[j * splits + i], &boxes[j * splits + i]);
		}
	}

	while(!KEY_Update())
	{
		//SCR_Clear();

		for(j = 0; j < splits; j++)
		{
			for(i = 0; i < splits; i++)
			{
				BOX_Clear(demos[j * splits + i].box);
				plugins[j * splits + i].DEMO_Frame(&demos[j * splits + i]);
				SWP_SetBox(cp.video.mode.current.xres / splits * i, cp.video.mode.current.yres / splits * j, demos[j * splits + i].box);
			}
		}

		SWP_Screen();
	}

	for(j = 0; j < splits; j++)
	{
		for(i = 0; i < splits; i++)
		{
			plugins[j * splits + i].DEMO_Final(&demos[j * splits + i]);
			BOX_Free(demos[j * splits + i].box);
		}
	}

	//SCR_Clear();
	CP_Final();

	return EXIT_SUCCESS;
}

#endif
